#include "stdafx.h"
#include "UserList.h"

/*
WOOT - NO ERROR CHECKING WHAT SO EVER
   this is horrible to do
   silently discarding errors
*/

using std::ifstream;
using std::ofstream;
using std::string;

CUserList::CUserList()
{
}

CUserList::~CUserList()
{
}

void CUserList::LoadUsersFromFile(char *szFileName, size_t ullSize)
{
   ifstream sInp;
   sInp.open(szFileName, std::ios::in | std::ios::binary);
   while ((sInp.eof() == false) && (sInp.fail() == false))
   {
      InternalUser sUser;
      char cptrData[sizeof(InternalUser)];
      sInp.read(cptrData, sizeof(InternalUser));
      if (sInp.fail() == false)
      {
         memcpy(&sUser, cptrData, sizeof(InternalUser));
         m_sVecUserList.push_back(sUser);
      }
   }
   sInp.close();
}

void CUserList::SaveUsersToFile(char *szFileName, size_t ullSize)
{
   ofstream sOut;
   sOut.open(szFileName, std::ios::out | std::ios::binary);
   for (int i = 0; i < m_sVecUserList.size(); i++)
   {
      InternalUser sUser = m_sVecUserList[i];
      char cptrData[sizeof(InternalUser)];
      memcpy(cptrData, &sUser, sizeof(InternalUser));
      sOut.write(cptrData, sizeof(InternalUser));
   }
   sOut.close();
}

bool CUserList::UserExists(char *szUserName, size_t ullUNSize)
{
   std::hash<string> sHashFn;
   InternalUser sUser;
   sUser.ullNameHash = sHashFn(string(szUserName));
   sUser.ullPassHash = 0;
   return (std::find(m_sVecUserList.begin(), m_sVecUserList.end(), sUser) != m_sVecUserList.end());
}

void CUserList::AddUser(char *szUserName, size_t ullUNSize)
{
   if (UserExists(szUserName, ullUNSize) == false)
   {
      InternalUser sUser;
      std::hash<string> sHashFn;
      sUser.ullNameHash = sHashFn(string(szUserName));
      sUser.ullPassHash = 0;
      m_sVecUserList.push_back(sUser);
   }
}

void CUserList::RemoveUser(char *szUserName, size_t ullUNSize)
{
   std::hash<string> sHashFn;
   InternalUser sUser;
   sUser.ullNameHash = sHashFn(string(szUserName));
   sUser.ullPassHash = 0;
   vector<InternalUser>::iterator sIt = std::find(m_sVecUserList.begin(), m_sVecUserList.end(), sUser);
   if (sIt != m_sVecUserList.end())
   {
      m_sVecUserList.erase(sIt);
   }
}

void CUserList::SetPass(char *szUserName, size_t ullUNSize, char *szPass, size_t ullPWSize)
{
   std::hash<string> sHashFn;
   InternalUser sUser;
   sUser.ullNameHash = sHashFn(string(szUserName));
   sUser.ullPassHash = 0;
   vector<InternalUser>::iterator sIt = std::find(m_sVecUserList.begin(), m_sVecUserList.end(), sUser);
   if (sIt != m_sVecUserList.end())
   {
      sIt->ullPassHash = sHashFn(string(szPass));
   }
}

bool CUserList::ValidateUser(char *szUserName, size_t ullUNSize, char *szPass, size_t ullPWSize)
{
   bool bRet = false;
   std::hash<string> sHashFn;
   InternalUser sUser;
   sUser.ullNameHash = sHashFn(string(szUserName));
   sUser.ullPassHash = sHashFn(string(szPass));
   vector<InternalUser>::iterator sIt = std::find(m_sVecUserList.begin(), m_sVecUserList.end(), sUser);
   if (sIt != m_sVecUserList.end())
   {
      bRet = (sIt->ullPassHash == sUser.ullPassHash);
   }
   return bRet;
}

void CUserList::ClearUsers()
{
   m_sVecUserList.clear();
}